﻿using System;
using System.Text;
using TSObjectLibrary;
using TSWindowLibrary;

namespace ExecuteTSScriptMethod
{
	class Program
	{
		static void Main(string[] args)
		{
			ExecuteMethod(args);
		}
		private static void ExecuteMethod(string[] args)
		{
			try
			{
				Log.Write(args[0]);
				Log.Write(args[1]);
				Log.Write(args[2]);
				Log.Write(args[3]);
				Log.Write(args[4]);
				var connector = new Connector();

				const AuthenticationModeEnum authMode = AuthenticationModeEnum.amDatabaseServer;

				connector.OpenConfigurationByName(Convert(args[0]), authMode, Convert(args[1]), Convert(args[2]));
				var Window = (Window)connector.Services.GetNewItemByUSI(Convert(args[3]));
				Window.ScriptControl.Run(Convert(args[4]));
			}
			catch(Exception e)
			{
				Log.Write(e.Message);
			}

		}

		private static string Convert(string p)
		{
			Encoding srcEncoding = Encoding.GetEncoding("ibm866");
			Encoding destEncoding = Encoding.GetEncoding("windows-1251");
			return srcEncoding.GetString(Encoding.Convert(destEncoding, srcEncoding, srcEncoding.GetBytes(p)));
		}


	}
}
